import { Injectable } from "@angular/core";
import { HttpClient, HttpParams } from "@angular/common/http";
import { Observable } from "rxjs";

@Injectable({
  providedIn: 'root'
})
export class OrderAccService {
  // 列表
  public GET_URL = "/order/list";
  // 批量审核
  public audit_URL = "/order/deliverySynchronize";
  // 导出主信息
  public export_URL = "/order/getOrderExcel";
  // 导出明细信息
  public getOrderDescExcel_URL = "/order/getOrderDescExcel";

  /**
   * 跟进查询
   */
  public followerList_URL = "/follower/list";
  /**
   * 文件上传地址
   */
  public FILE_URL = "/common/upload/file";
  /**
   * 新增跟进
   */
  public followerSave_URL = "/follower/save";

  // 查询渠道(字典)
  public dict_URL = '/dict/detail';

  // 配件订单删除
  orderDeleteUrl = '/order/delete';

  // 确认收款
  confirmCollectionUrl = '/order/confirmReceipt';

  constructor(public httpClient: HttpClient) { }

  /**
   * 上传文件
   * @param fileToUpload 
   * @returns 
   */
  public postFile(fileToUpload: File, fileName?: any): Observable<any> {
    const formData: FormData = new FormData();
    formData.append('file', fileToUpload);
    formData.append('name', "order");
    if (fileName) {
      formData.append('fileName', fileName);
    }
    
    return this.httpClient.post(this.FILE_URL, formData);
  }
  /**
   * 新增跟进
   */
  public followerSave(json: any | null): Observable<any> {
    let jsons = { ...json };
    jsons.enclosure = jsons.enclosure.map((ele: any) => {
      return ele.url
    }).join(',');

    return this.httpClient.post(this.followerSave_URL, jsons);
  }
  /**
   * 跟进查询
   */
  public followerLst(json: any | null): Observable<any> {
    return this.httpClient.post(this.followerList_URL, json);
  }
  /**
  * 列表
  * @param json 参数
  */
  public get(json: any | null): Observable<any> {
    let jsons = { ...json };
    jsons.orderNum = jsons.orderNum.trim();
    jsons.goodsName = jsons.goodsName.trim();
    if (jsons?.dateFormat?.length != 0) {
      jsons.orderBeginTime = this.shiftDate(jsons.dateFormat[0]);
      jsons.orderEndTime = this.shiftDate(jsons.dateFormat[1]);
      delete jsons.dateFormat;
    }
    if (jsons.paymentTime?.length != 0) {
      jsons.paymentBeginTime = this.shiftDate(jsons.paymentTime[0]);
      jsons.paymentEndTime = this.shiftDate(jsons.paymentTime[1]);
      delete jsons.paymentTime;
    }
    if (jsons.completeTime?.length != 0) {
      jsons.dealBeginTime = this.shiftDate(jsons.completeTime[0]);
      jsons.dealEndTime = this.shiftDate(jsons.completeTime[1]);
      delete jsons.completeTime;
    }
    if (jsons.shipTime?.length != 0) {
      jsons.shipBeginTime = this.shiftDate(jsons.shipTime[0]);
      jsons.shipEndTime = this.shiftDate(jsons.shipTime[1]);
      delete jsons.shipTime;
    }
    return this.httpClient.post(this.GET_URL, jsons);
  }
  /**
  * 导出
  * @param json 参数
  */
  public export(json: any | null): Observable<any> {
    let jsons = { ...json };
    let url = "";
    if (jsons.exportCode == 1) {
      url = this.export_URL;
    } else {
      url = this.getOrderDescExcel_URL;
    }
    if (jsons?.dateFormat?.length != 0) {
      jsons.orderBeginTime = this.shiftDate(jsons.dateFormat[0]);
      jsons.orderEndTime = this.shiftDate(jsons.dateFormat[1]);
      delete jsons.dateFormat;
    }
    if (jsons.paymentTime?.length != 0) {
      jsons.paymentBeginTime = this.shiftDate(jsons.paymentTime[0]);
      jsons.paymentEndTime = this.shiftDate(jsons.paymentTime[1]);
      delete jsons.paymentTime;
    }
    if (jsons.completeTime?.length != 0) {
      jsons.dealBeginTime = this.shiftDate(jsons.completeTime[0]);
      jsons.dealEndTime = this.shiftDate(jsons.completeTime[1]);
      delete jsons.completeTime;
    }
    if (jsons.shipTime?.length != 0) {
      jsons.shipBeginTime = this.shiftDate(jsons.shipTime[0]);
      jsons.shipEndTime = this.shiftDate(jsons.shipTime[1]);
      delete jsons.shipTime;
    }
    delete jsons.exportCode;
    for (let key in jsons) {
      if (jsons[key] == '' || jsons[key] == null || jsons[key] == undefined) {
        delete jsons[key];
      }
    }
    return this.httpClient.get(url, { params: jsons });
  }
  /**
  * 批量审核
  * @param json 参数
  */
  public audit(json: any | null): Observable<any> {
    return this.httpClient.post(this.audit_URL, json);
  }
  /**
   * 时间格式转换 年月日
   * @param date 
   * @returns 
   */
  shiftDate(date: any) {
    let d = new Date(date)
    let str = d.getFullYear() + '-' + this.p((d.getMonth() + 1)) + '-' + this.p(d.getDate());
    return str;
  }
  /**
   * 补0
   * @param s 
   */
  p(s: any) {
    return s < 10 ? '0' + s : s
  }

  // 查询渠道（字典）
  public async dictData(parentKey: any) {
    const params: any = {
      pageNum: 1,
      pageSize: 999,
      parentKey
    }
    return this.httpClient.get(this.dict_URL, { params }).toPromise();
  }

  /**
   * 配件订单删除
   * @param params 
   * @returns 
   */
  orderDeleteItem(params: any) {
    return this.httpClient.post(this.orderDeleteUrl, params).toPromise();
  }

  // 确认收款
  confirmCollectionData(id: number) {
    // let params = {
    //   id
    // }
    return this.httpClient.post(this.confirmCollectionUrl, { id }).toPromise();
  }
}
